package com.crm.system.service.impl.contractManager.statistics;

import com.crm.model.entity.reportStatistics.EchartModel;
import com.crm.model.entity.reportStatistics.Series;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.customerContractManager.ContractStatisticsVO;
import com.crm.service.contractManager.statistics.CustomerContractStatisticsDbService;
import com.crm.system.service.contractManager.statistics.CustomerContractStatisticsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yincuimei
 * @Date 2021/4/22 9:54
 */
@Service
public class CustomerContractStatisticsServiceImpl implements CustomerContractStatisticsService {
    @Autowired
    private CustomerContractStatisticsDbService contractStatisticsDbService;

    /**
     * 新会员分析: 按本年度、上年度统计新增会员数、成交顾客数、新会员招募率(计算公式-新增会员数/成交顾客数)
     * @param dateUnit     ---日期类型(1:本年度;2:上年度)
     * @author yincuimei
     * @return
     */
    @Override
    public EchartModel selectNewMembersAndDealCountByYear(Integer dateUnit) {
        EchartModel echartModel = new EchartModel();
        echartModel.setXAxis(new ArrayList<>());//x轴
        echartModel.setSeries(new ArrayList<>());
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息



        //新增会员数(合同表查生效状态为生效中，合同类型为会员新单合同的合同数量)
        List<ContractStatisticsVO> newMembersCountList = contractStatisticsDbService.selectNewMembersAndDealCountByYear(userAccount.getCompanyId(),dateUnit,"2");

        //新增会员数data
        Series<Integer> newMembersCountSeries = new Series<>();
        if(newMembersCountList != null && newMembersCountList.size() > 0){
            newMembersCountSeries.setName("新增会员数");
            newMembersCountSeries.setData(new ArrayList<Integer>());
            for(ContractStatisticsVO contractStatisticsVO : newMembersCountList){
                newMembersCountSeries.getData().add(contractStatisticsVO.getNumData());//新增会员数
            }
           echartModel.getSeries().add(newMembersCountSeries);
        }

        //成交顾客数(合同表查生效状态为生效中的合同数量)
        List<ContractStatisticsVO> dealCountList = contractStatisticsDbService.selectNewMembersAndDealCountByYear(userAccount.getCompanyId(),dateUnit,null);

        //成交顾客数data
        Series<Integer> dealCountSeries = new Series<>();
        Series<Double> recruitmentRateSeries = new Series<Double>();
        if(dealCountList != null & dealCountList.size() > 0){
            dealCountSeries.setName("成交顾客数");
            dealCountSeries.setData(new ArrayList<Integer>());
            recruitmentRateSeries.setName("新会员招募率");
            recruitmentRateSeries.setData(new ArrayList<>());
            for(int index = 0; index < dealCountList.size(); index++){
                ContractStatisticsVO contractStatisticsVO =  dealCountList.get(index);
                echartModel.getXAxis().add(contractStatisticsVO.getTimeData());//x轴
                dealCountSeries.getData().add(contractStatisticsVO.getNumData());//成交顾客数

                //新会员招募率:(计算公式-新增会员数/成交顾客数)
                if(newMembersCountList != null && newMembersCountList.size() > 0 && newMembersCountList.get(index) != null){
                    ContractStatisticsVO statisticsVO = newMembersCountList.get(index);
                    if(contractStatisticsVO.getNumData() > 0 && statisticsVO.getNumData() > 0){
                        Integer newMembersCount = statisticsVO.getNumData();//新增会员数
                        Integer dealCount = contractStatisticsVO.getNumData();//成交顾客数
                        BigDecimal newMembersCountData =  new BigDecimal(newMembersCount);
                        BigDecimal dealCountData =  new BigDecimal(dealCount);
                        Double recruitmentRateData = newMembersCountData.divide(dealCountData,2,BigDecimal.ROUND_HALF_UP).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
                        recruitmentRateSeries.getData().add(recruitmentRateData);//新会员招募率
                    }else{
                        recruitmentRateSeries.getData().add(Double.valueOf("0"));
                    }
                }
            }
            echartModel.getSeries().add(dealCountSeries);//成交顾客数
            echartModel.getSeries().add(recruitmentRateSeries);//新会员招募率
        }
        return echartModel;
    }

}
